package cn.itcast.bos.base.action;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;

import com.github.stuxuhai.jpinyin.PinyinHelper;

import cn.itcast.bos.base.service.SubAreaService;
import cn.itcast.bos.domain.base.Area;
import cn.itcast.bos.domain.base.FixedArea;
import cn.itcast.bos.domain.base.SubArea;


@Namespace("/")
@ParentPackage("json-default")
@Controller
@Scope("prototype")
public class SubAreaAction extends BaseAction<SubArea>{
	private File file;
	private String fileFileName;
	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}

	public void setFile(File file) {
		this.file = file;
	}

	@Autowired
	private SubAreaService subareaservice;
	
	@Action(value="searchSubArea",results={@Result(type="json",name="success")})
	public String searchSubArea(){
		Pageable pageable=new PageRequest(page-1, rows);
		Page<SubArea> page= subareaservice.page(pageable);
		putIntoValueStack(page);
		return SUCCESS;
	}
	
	@Action(value="uploadsubareaExcel",results={@Result(name="success",type="redirect",location="/pages/base/sub_area.html")})
	public String uploadsubareaExcel() throws Exception{
		Workbook wb=new XSSFWorkbook(file);
		int sheets = wb.getNumberOfSheets();
		List<SubArea> list=new ArrayList<SubArea>();
		for (int i = 0; i < sheets; i++) {
			Sheet sheet = wb.getSheetAt(i);
			for (Row row : sheet) {
				if(row.getRowNum()==0){
					continue;
				}
				if(row.getCell(0)==null||StringUtils.isBlank(row.getCell(0).getStringCellValue())){
					continue;
				}
				SubArea subarea=new SubArea();
				subarea.setId(row.getCell(0).getStringCellValue());
				FixedArea fixedArea=new FixedArea();
				fixedArea.setId(row.getCell(1).getStringCellValue());
				subarea.setFixedArea(fixedArea);
				Area area=new Area();
				area.setId(row.getCell(2).getStringCellValue());
				subarea.setArea(area);
				String keyword=row.getCell(3).getStringCellValue();
				subarea.setKeyWords(keyword);
				String startNum=row.getCell(4).getStringCellValue();
				subarea.setStartNum(startNum);
				String endNum=row.getCell(5).getStringCellValue();
				subarea.setEndNum(endNum);
				subarea.setSingle(row.getCell(6).getStringCellValue().charAt(0));
				StringBuilder builder=new StringBuilder();
				builder.append(keyword).append(startNum).append("/").append(endNum);
				subarea.setAssistKeyWords(builder.toString());
				list.add(subarea);
			}
		}
		subareaservice.uploadsubareaExcel(list);
		return SUCCESS;
	} 
}
